package com.efounder.util;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

/**
 * 
 * @author fengshuquan
 * 
 * @RemoteView 使用方法
 * cameraUtil = new CameraUtil(TabletHomeActivity.this);	
 * cameraUtil.takePhoto("tablet", "a.png");	
 * 复写TabletHomeActivity的onActivityResult方法
 * if (resultCode != Activity.RESULT_OK) {
 * 		return;
 * }
 * if (resultCode == CameraUtil.FROM_CAMERA) {
 * 		
 * 		imageButton.setImageBitmap(cameraUtil.getPhotoBitmap);
 * }
 *
 */

public class CameraUtil {
	
	public CameraUtil(Activity context) {
		super();
		this.context = context;
	}

	private Activity context = null;
	private String filePath;

	public void takePhoto(String name) {
		File f = MediaEnvironment.getPhotoDir();
		filePath = f.getPath() +"/" + name +  MediaEnvironment.PHOTO_EXT;
		File photoFlie = new File(f, name + MediaEnvironment.PHOTO_EXT);
		Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE,null);
		cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFlie));
		context.startActivityForResult(cameraIntent, MediaEnvironment.PHOTO_CODE);
	}
	
	public void takeVideo(String name) {
		File f = MediaEnvironment.getVideoDir();
		filePath = f.getPath() +"/" + name +  MediaEnvironment.VIDEO_EXT;
		File photoFlie = new File(f, name + MediaEnvironment.VIDEO_EXT);
		Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFlie));
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
		context.startActivityForResult(intent, MediaEnvironment.VIDEO_CODE);
	}
	
	public Bitmap getPhotoBitmap(){
		Bitmap bitmap = BitmapFactory.decodeFile(filePath);
		return bitmap;
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	}
}